package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Gnarly"}, name="SystemUpdate", version=1.4D)
public class SystemUpdate extends Random
{
  private int logoutMinutes;
  private int logoutSeconds;

  public boolean activateCondition()
  {
    if ((this.game.isLoggedIn()) && (this.interfaces.getComponent(754, 5).getText().startsWith("<col=ffff00>System update in")) && (!getMyPlayer().isInCombat())) {
      check();
    }
    return false;
  }

  public int loop()
  {
    return -1;
  }

  private void check() {
    this.logoutMinutes = random(1, getMinutes());
    this.logoutSeconds = random(10, getSeconds());
    while (!checkForLogout()) {
      sleep(1000);
    }
    this.log.info("System update will occur soon, we have logged out.");
  }

  private boolean checkForLogout() {
    if ((getMinutes() < this.logoutMinutes) && (getSeconds() < this.logoutSeconds)) {
      stopScript(false);
      return true;
    }
    return false;
  }

  private int getMinutes() {
    return Integer.parseInt(this.interfaces.getComponent(754, 5).getText().substring(29).trim().split(":")[0]);
  }

  private int getSeconds() {
    return Integer.parseInt(this.interfaces.getComponent(754, 5).getText().substring(29).trim().split(":")[1]);
  }
}